//%%%%%%%%%%%%%%%%%//
// OLG Dynare File //
//%%%%%%%%%%%%%%%%%//

compute_ss     = 0 ;

if ~exist('year_') ;          year_  = '1940' ; end
if ~exist('const_prob_gam') ; const_prob_gam  = 0 ; end
if ~exist('gov') ;            gov  = 1 ; end
if ~exist('flata') ;          flata  = 0 ; end

toload = ['_gam', year_] ;

if const_prob_gam ; toload = [toload, '_const_prob_gam'] ; end
if ~gov           ; toload = [toload, '_nogov'] ; end
if flata          ; toload = [toload, '_flata'] ; end

N_g     = 80 ; 
N_g_ben = 49 ; 
ret_T   = 80 ; 

@# define N_g     = 80
@# define N_g_ben = 49
@# define ret_T   = 80
@# define n_g     = 1:N_g

SET.N_g   = N_g ;
SET.ret_T = ret_T ;

//******************//
// Define variables //
//******************//

@# for j in 1 : N_g
    var n@{j} n@{j}_ ;
@# endfor

@# for j in 1 : N_g
    var c@{j} ;
@# endfor

@# for j in 1 : N_g-1
    var k@{j} ;
@# endfor

var n n_ c k w r R y a g tax tran;
var dy ;
var check1 resid_k ;
var d pr ;

parameters rhoa delta ;

@# for j in 1 : N_g
    parameters a@{j} ;
@# endfor

parameters beta alpha gy nu ;
parameters varphi ;
parameters n1ss ass ;

@# for j in 1 : N_g
    parameters v@{j} gam@{j} ;
@# endfor

@# for j in 1 : N_g
parameters ret_t@{j} aux@{j} ;
ret_t@{j} = ret_T ;
    @# for jj in 1 : N_g
        parameters aux@{j}_@{jj} ;
        @# if jj<ret_T
            aux@{j}_@{jj} = 1 ;
        @# else
            aux@{j}_@{jj} = 0 ;
        @# endif
    @# endfor
@# endfor

parameters thetap elasp lambdap ;

@# for j in 1 : N_g
varexo eps_n@{j} gam@{j}_ v@{j}_ ;
@# endfor

//*******************************//
// Set Parameters and Compute SS //
//*******************************//

n_g_bw = (N_g-1):-1:1 ;

for jj=1:(N_g+1)
    if jj<(N_g+1)
        eval(['cvecf{', int2str(jj), '} = ''c', int2str(jj),'f(kk)'' ;']) ;
        eval(['nvecf{', int2str(jj), '} = ''n', int2str(jj),'ss'' ;']) ;
    else
        eval(['cvecf{', int2str(jj), '} = ''', int2str(0),''' ;']) ;
        eval(['nvecf{', int2str(jj), '} = ''', int2str(0),''' ;']) ;
    end 

    if jj<N_g
        eval(['wvecf{', int2str(jj), '} = ''aux', int2str(n_g_bw(jj)), '*w', int2str(n_g_bw(jj)),'f(kk)'' ;']) ;
        eval(['evecf{', int2str(jj), '} = ''aux', int2str(n_g_bw(jj)), '*w', int2str(n_g_bw(jj)),'f(kk)*(1-gy) + (1-aux', int2str(n_g_bw(jj)), ')*0.0'' ;']) ;
    else
        eval(['wvecf{', int2str(jj), '} = ''', int2str(0),''' ;']) ;
        eval(['evecf{', int2str(jj), '} = ''', int2str(0),''' ;']) ;
    end 
end

beta  = 0.99775^(4*80/N_g) ;
alpha = 1/3 ; 
delta = 1-(1-0.0275)^(4*80/N_g) ; 
nu    = 2 ;
gy    = 0.4*gov ;
rhoa  = 0.8^(4*80/N_g) ;

thetap  = 0 ;  
elasp   = 0 ;   
lambdap = 0 ;

varphi  = 2 ;

ass     = 1 ;
n1_ss   = 1 ;

pk_vec  = 1 ;

//*************//
// LOAD GAMMAS //
//*************//

get_gam_probs

eval(['gamvec = gam_mat(:,', ...
    'idxt_new(timevec_dat==', year_, ')) ;']) ;

if const_prob_gam 
    gamvec = 0.012987012987013 + 0*gamvec ;
end

get_a_vec
vvec_setup

gamvec = interp1((1:length(gamvec))',gamvec',linspace(1,length(gamvec),N_g)') ;
avec = amat(:,1) ;
vvec = interp1((1:length(vvec))',vvec',linspace(1,length(vvec),N_g)') ;

for j=1:N_g
    eval(['ret_t', int2str(j), '=ret_T ;']) ;
    for jj=1:N_g
        if jj<ret_T
            eval(['aux', int2str(j), '_', int2str(jj), '=1 ;']) ;
        else
            eval(['aux', int2str(j), '_', int2str(jj), '=0 ;']) ;
        end
    end
end

for j=1:N_g
    eval(['a', int2str(j), '=avec(', int2str(j), ');']) ;
end

for j=1:N_g
    eval(['gam', int2str(j), '=gamvec(', int2str(j), ');']) ;
end

for j=1:N_g
    eval(['v', int2str(j), '=vvec(', int2str(j), ');']) ;
end

for j=1:N_g
    eval(['aux',int2str(j),'= aux',int2str(j),'_',int2str(j),';']) ;
end

for jj=2:N_g
    eval(['n', int2str(jj), '_ss = n', int2str(jj-1), '_ss*(1-gam', int2str(jj-1),') ;']) ;
end

for jj=1:N_g
    eval(['n', int2str(jj), 'ss = aux', int2str(jj), ' * a', int2str(jj), ' * n', int2str(jj), '_ss ;']) ;
end

nss = 0 ;
for jj=1:(N_g-1)
    eval(['nss = nss + n', int2str(jj),'ss ; ']) ;
end

n_ss = 0 ;
for jj=1:(N_g-1)
    eval(['n_ss = n_ss + n', int2str(jj),'_ss ; ']) ;
end

prss = 0 ;
for jj=1:N_g
    eval(['prss = prss + aux', int2str(jj), '*n', int2str(jj),'_ss ; ']) ;
end

prss = prss / n_ss ;

@# for j in 1:N_g
    gam@{j} = gamvec(@{j}) ;
    a@{j}   = avec(@{j}) ;
    v@{j}   = vvec(@{j}) ;
    aux@{j} = aux@{j}_@{j} ;
@# endfor

@# for j in 2:N_g
    n@{j}_ss = n@{j-1}_ss*(1-gam@{j-1}) ;
    n@{j}ss  = aux@{j}*a@{j}*n@{j}_ss ;
@# endfor

n1ss    = 1 ;

var b, l, tau ;

@# for j in 1 : N_g-1
    var ss@{j} tran@{j} ;
@# endfor

@# for j in 1 : N_g-1
    varexo ret_aux@{j} ;
@# endfor

@# for j in 1 : N_g
    varexo a@{j}_ ;
@# endfor

varexo rr ;

varexo Tstar ;

//*******//
// Model //
//*******//

model ;

// Interest rate equilibrium
R = 1+r(+1)-delta ;

// Total output
y = a^(1-alpha) * n^(1-alpha) * (k(-1))^alpha ; 

// Wages
w = a^(1-alpha) * n^(-alpha)  * (1-alpha) * (k(-1))^alpha ;

// Return on capital
r = a^(1-alpha) * n^(1-alpha) * alpha * (k(-1))^(alpha-1) ;

// Dividends per capita, to be redistributed to workers
d = 0 ; 

tax = 
@# for j in 1:ret_T-1
    aux@{j} * gy * w * a@{j}_ * n@{j} + 
@# endfor
0 ;

tran = tax / (pr) ;

ss1 = (w * a1_ * n1) ;

@# for j in 2:N_g-1
    ss@{j} = (1-ret_aux@{j})*(ss@{j-1}(-1) + (w * a@{j}_ * n@{j})) + ret_aux@{j}*(ss@{j-1}(-1)) ;
@# endfor

@# for j in 1:N_g-1
    tran@{j} = ret_aux@{j} * 0.467 * ss@{j} / 48 ; 
@# endfor

// Government budget constraint
0 = 
@# for j in 1:N_g-1
    tau * (n@{j}_ * w * a@{j}_ * n@{j} ) - n@{j}_ * tran@{j} + 
@# endfor
0 ;

// Goods market clearing
y = k - (1-delta)*k(-1) + c + g ; 

// Total consumption
c = 
@# for j in 1:N_g
    n@{j}_ * c@{j} + 
@# endfor
0 ;

(c1^(-nu) - beta * (1+r(+1)-delta) * c2(+1)^(-nu) ) = 0 ;
@# for j in 1:N_g-2
    (c@{j+1}^(-nu)- beta * (1+r(+1)-delta) * (c@{j+2}(+1))^(-nu) ) = 0 ;
@# endfor

// Budget constraint, for t=1
c1 = aux1 * (w * a1_ * n1)*(1-tau) - k1 + d ; 

// Actual budget constraint, for t=2,...,T-1, 
@# for j in 1:N_g-2
    c@{j+1} = aux@{j+1} * (w * a@{j+1}_ * n@{j+1})*(1-tau) + (1/(1-gam@{j}_(-1)))*(1+r-delta)*k@{j}(-1) - k@{j+1} + (1-ret_aux@{j})*d + ret_aux@{j}*tran@{j} ;
@# endfor

// Actual budget constraint, for t=T
@# for j in (N_g-1):(N_g-1)
    c@{j+1} = (1/(1-gam@{j}_(-1)))*(1+r-delta)*k@{j}(-1) - b ;
@# endfor

b = 0.0001 ;

// Total capital
k = 
@# for j in 1:N_g-1
    n@{j}_ * k@{j} + 
@# endfor
0 ;

// Total effective labor
n = 
@# for j in 1:N_g-1
    n@{j}_ * a@{j}_ * n@{j} + 
@# endfor
0 ;

// Total number of possible workers
n_ = 
@# for j in 1:N_g-1
    n@{j}_ + 
@# endfor
0 ;

// Labor supplied, depending on retired
@# for j in 1:N_g
    n@{j} = min(aux@{j} * (( a@{j}_ * w ) * c@{j}^(-nu) / v@{j}_)^(1/varphi), aux@{j}) ;
@# endfor

// Total a
a = ass^(1-rhoa) * a(-1)^(rhoa) ;

// Demographics
n1_ = n1ss + eps_n1 ;
@# for j in 2:N_g
    n@{j}_ = n@{j-1}_(-1) * (1-gam@{j-1}_) + eps_n@{j} ;
@# endfor

// Amount of capital bequested on death 
resid_k = 
@# for j in 1:N_g-1
    gam@{j}_ * n@{j}_ * k@{j} + 
@# endfor
0 ;

pr = 
@# for j in 1:N_g-1
    n@{j}_ + 
@# endfor
0 ;

// Participation rate
l = 
@# for j in 1:ret_T-1
    n@{j}_ * (1/n_) * n@{j} + 
@# endfor
0 ;

dy = log(y/y(-1)) ;

check1 = y - (k - (1-delta)*k(-1) + c + g) ;

end ;

tv_nshocks = 1 ;
longev_sh  = 1 ;

//********************//
// Set starting value //
//********************//

load ss_out_bc
ss_out = ss_out ;

get_gam_probs ;

if longev_sh==0 ;
gam_mat(:,1:131) = repmat(gam_mat(:,1),[1 131]) ;
end

get_rr_vec ;

get_a_vec ;

rr_vec_ = rr_vec ;

initval ; 

n1=ss_out(1) ; n1_=ss_out(2) ; n2=ss_out(3) ; n2_=ss_out(4) ; n3=ss_out(5) ; n3_=ss_out(6) ; n4=ss_out(7) ; n4_=ss_out(8) ; n5=ss_out(9) ; n5_=ss_out(10) ; n6=ss_out(11) ; n6_=ss_out(12) ; n7=ss_out(13) ; n7_=ss_out(14) ; n8=ss_out(15) ; n8_=ss_out(16) ; n9=ss_out(17) ; n9_=ss_out(18) ; n10=ss_out(19) ; n10_=ss_out(20) ; n11=ss_out(21) ; n11_=ss_out(22) ; n12=ss_out(23) ; n12_=ss_out(24) ; n13=ss_out(25) ; n13_=ss_out(26) ; n14=ss_out(27) ; n14_=ss_out(28) ; n15=ss_out(29) ; n15_=ss_out(30) ; n16=ss_out(31) ; n16_=ss_out(32) ; n17=ss_out(33) ; n17_=ss_out(34) ; n18=ss_out(35) ; n18_=ss_out(36) ; n19=ss_out(37) ; n19_=ss_out(38) ; n20=ss_out(39) ; n20_=ss_out(40) ; n21=ss_out(41) ; n21_=ss_out(42) ; n22=ss_out(43) ; n22_=ss_out(44) ; n23=ss_out(45) ; n23_=ss_out(46) ; n24=ss_out(47) ; n24_=ss_out(48) ; n25=ss_out(49) ; n25_=ss_out(50) ; n26=ss_out(51) ; n26_=ss_out(52) ; n27=ss_out(53) ; n27_=ss_out(54) ; n28=ss_out(55) ; n28_=ss_out(56) ; n29=ss_out(57) ; n29_=ss_out(58) ; n30=ss_out(59) ; n30_=ss_out(60) ; n31=ss_out(61) ; n31_=ss_out(62) ; n32=ss_out(63) ; n32_=ss_out(64) ; n33=ss_out(65) ; n33_=ss_out(66) ; n34=ss_out(67) ; n34_=ss_out(68) ; n35=ss_out(69) ; n35_=ss_out(70) ; n36=ss_out(71) ; n36_=ss_out(72) ; n37=ss_out(73) ; n37_=ss_out(74) ; n38=ss_out(75) ; n38_=ss_out(76) ; n39=ss_out(77) ; n39_=ss_out(78) ; n40=ss_out(79) ; n40_=ss_out(80) ; n41=ss_out(81) ; n41_=ss_out(82) ; n42=ss_out(83) ; n42_=ss_out(84) ; n43=ss_out(85) ; n43_=ss_out(86) ; n44=ss_out(87) ; n44_=ss_out(88) ; n45=ss_out(89) ; n45_=ss_out(90) ; n46=ss_out(91) ; n46_=ss_out(92) ; n47=ss_out(93) ; n47_=ss_out(94) ; n48=ss_out(95) ; n48_=ss_out(96) ; n49=ss_out(97) ; n49_=ss_out(98) ; n50=ss_out(99) ; n50_=ss_out(100) ; n51=ss_out(101) ; n51_=ss_out(102) ; n52=ss_out(103) ; n52_=ss_out(104) ; n53=ss_out(105) ; n53_=ss_out(106) ; n54=ss_out(107) ; n54_=ss_out(108) ; n55=ss_out(109) ; n55_=ss_out(110) ; n56=ss_out(111) ; n56_=ss_out(112) ; n57=ss_out(113) ; n57_=ss_out(114) ; n58=ss_out(115) ; n58_=ss_out(116) ; n59=ss_out(117) ; n59_=ss_out(118) ; n60=ss_out(119) ; n60_=ss_out(120) ; n61=ss_out(121) ; n61_=ss_out(122) ; n62=ss_out(123) ; n62_=ss_out(124) ; n63=ss_out(125) ; n63_=ss_out(126) ; n64=ss_out(127) ; n64_=ss_out(128) ; n65=ss_out(129) ; n65_=ss_out(130) ; n66=ss_out(131) ; n66_=ss_out(132) ; n67=ss_out(133) ; n67_=ss_out(134) ; n68=ss_out(135) ; n68_=ss_out(136) ; n69=ss_out(137) ; n69_=ss_out(138) ; n70=ss_out(139) ; n70_=ss_out(140) ; n71=ss_out(141) ; n71_=ss_out(142) ; n72=ss_out(143) ; n72_=ss_out(144) ; n73=ss_out(145) ; n73_=ss_out(146) ; n74=ss_out(147) ; n74_=ss_out(148) ; n75=ss_out(149) ; n75_=ss_out(150) ; n76=ss_out(151) ; n76_=ss_out(152) ; n77=ss_out(153) ; n77_=ss_out(154) ; n78=ss_out(155) ; n78_=ss_out(156) ; n79=ss_out(157) ; n79_=ss_out(158) ; n80=ss_out(159) ; n80_=ss_out(160) ; c1=ss_out(161) ; c2=ss_out(162) ; c3=ss_out(163) ; c4=ss_out(164) ; c5=ss_out(165) ; c6=ss_out(166) ; c7=ss_out(167) ; c8=ss_out(168) ; c9=ss_out(169) ; c10=ss_out(170) ; c11=ss_out(171) ; c12=ss_out(172) ; c13=ss_out(173) ; c14=ss_out(174) ; c15=ss_out(175) ; c16=ss_out(176) ; c17=ss_out(177) ; c18=ss_out(178) ; c19=ss_out(179) ; c20=ss_out(180) ; c21=ss_out(181) ; c22=ss_out(182) ; c23=ss_out(183) ; c24=ss_out(184) ; c25=ss_out(185) ; c26=ss_out(186) ; c27=ss_out(187) ; c28=ss_out(188) ; c29=ss_out(189) ; c30=ss_out(190) ; c31=ss_out(191) ; c32=ss_out(192) ; c33=ss_out(193) ; c34=ss_out(194) ; c35=ss_out(195) ; c36=ss_out(196) ; c37=ss_out(197) ; c38=ss_out(198) ; c39=ss_out(199) ; c40=ss_out(200) ; c41=ss_out(201) ; c42=ss_out(202) ; c43=ss_out(203) ; c44=ss_out(204) ; c45=ss_out(205) ; c46=ss_out(206) ; c47=ss_out(207) ; c48=ss_out(208) ; c49=ss_out(209) ; c50=ss_out(210) ; c51=ss_out(211) ; c52=ss_out(212) ; c53=ss_out(213) ; c54=ss_out(214) ; c55=ss_out(215) ; c56=ss_out(216) ; c57=ss_out(217) ; c58=ss_out(218) ; c59=ss_out(219) ; c60=ss_out(220) ; c61=ss_out(221) ; c62=ss_out(222) ; c63=ss_out(223) ; c64=ss_out(224) ; c65=ss_out(225) ; c66=ss_out(226) ; c67=ss_out(227) ; c68=ss_out(228) ; c69=ss_out(229) ; c70=ss_out(230) ; c71=ss_out(231) ; c72=ss_out(232) ; c73=ss_out(233) ; c74=ss_out(234) ; c75=ss_out(235) ; c76=ss_out(236) ; c77=ss_out(237) ; c78=ss_out(238) ; c79=ss_out(239) ; c80=ss_out(240) ; k1=ss_out(241) ; gam1_=ss_out(242) ; k2=ss_out(243) ; gam2_=ss_out(244) ; k3=ss_out(245) ; gam3_=ss_out(246) ; k4=ss_out(247) ; gam4_=ss_out(248) ; k5=ss_out(249) ; gam5_=ss_out(250) ; k6=ss_out(251) ; gam6_=ss_out(252) ; k7=ss_out(253) ; gam7_=ss_out(254) ; k8=ss_out(255) ; gam8_=ss_out(256) ; k9=ss_out(257) ; gam9_=ss_out(258) ; k10=ss_out(259) ; gam10_=ss_out(260) ; k11=ss_out(261) ; gam11_=ss_out(262) ; k12=ss_out(263) ; gam12_=ss_out(264) ; k13=ss_out(265) ; gam13_=ss_out(266) ; k14=ss_out(267) ; gam14_=ss_out(268) ; k15=ss_out(269) ; gam15_=ss_out(270) ; k16=ss_out(271) ; gam16_=ss_out(272) ; k17=ss_out(273) ; gam17_=ss_out(274) ; k18=ss_out(275) ; gam18_=ss_out(276) ; k19=ss_out(277) ; gam19_=ss_out(278) ; k20=ss_out(279) ; gam20_=ss_out(280) ; k21=ss_out(281) ; gam21_=ss_out(282) ; k22=ss_out(283) ; gam22_=ss_out(284) ; k23=ss_out(285) ; gam23_=ss_out(286) ; k24=ss_out(287) ; gam24_=ss_out(288) ; k25=ss_out(289) ; gam25_=ss_out(290) ; k26=ss_out(291) ; gam26_=ss_out(292) ; k27=ss_out(293) ; gam27_=ss_out(294) ; k28=ss_out(295) ; gam28_=ss_out(296) ; k29=ss_out(297) ; gam29_=ss_out(298) ; k30=ss_out(299) ; gam30_=ss_out(300) ; k31=ss_out(301) ; gam31_=ss_out(302) ; k32=ss_out(303) ; gam32_=ss_out(304) ; k33=ss_out(305) ; gam33_=ss_out(306) ; k34=ss_out(307) ; gam34_=ss_out(308) ; k35=ss_out(309) ; gam35_=ss_out(310) ; k36=ss_out(311) ; gam36_=ss_out(312) ; k37=ss_out(313) ; gam37_=ss_out(314) ; k38=ss_out(315) ; gam38_=ss_out(316) ; k39=ss_out(317) ; gam39_=ss_out(318) ; k40=ss_out(319) ; gam40_=ss_out(320) ; k41=ss_out(321) ; gam41_=ss_out(322) ; k42=ss_out(323) ; gam42_=ss_out(324) ; k43=ss_out(325) ; gam43_=ss_out(326) ; k44=ss_out(327) ; gam44_=ss_out(328) ; k45=ss_out(329) ; gam45_=ss_out(330) ; k46=ss_out(331) ; gam46_=ss_out(332) ; k47=ss_out(333) ; gam47_=ss_out(334) ; k48=ss_out(335) ; gam48_=ss_out(336) ; k49=ss_out(337) ; gam49_=ss_out(338) ; k50=ss_out(339) ; gam50_=ss_out(340) ; k51=ss_out(341) ; gam51_=ss_out(342) ; k52=ss_out(343) ; gam52_=ss_out(344) ; k53=ss_out(345) ; gam53_=ss_out(346) ; k54=ss_out(347) ; gam54_=ss_out(348) ; k55=ss_out(349) ; gam55_=ss_out(350) ; k56=ss_out(351) ; gam56_=ss_out(352) ; k57=ss_out(353) ; gam57_=ss_out(354) ; k58=ss_out(355) ; gam58_=ss_out(356) ; k59=ss_out(357) ; gam59_=ss_out(358) ; k60=ss_out(359) ; gam60_=ss_out(360) ; k61=ss_out(361) ; gam61_=ss_out(362) ; k62=ss_out(363) ; gam62_=ss_out(364) ; k63=ss_out(365) ; gam63_=ss_out(366) ; k64=ss_out(367) ; gam64_=ss_out(368) ; k65=ss_out(369) ; gam65_=ss_out(370) ; k66=ss_out(371) ; gam66_=ss_out(372) ; k67=ss_out(373) ; gam67_=ss_out(374) ; k68=ss_out(375) ; gam68_=ss_out(376) ; k69=ss_out(377) ; gam69_=ss_out(378) ; k70=ss_out(379) ; gam70_=ss_out(380) ; k71=ss_out(381) ; gam71_=ss_out(382) ; k72=ss_out(383) ; gam72_=ss_out(384) ; k73=ss_out(385) ; gam73_=ss_out(386) ; k74=ss_out(387) ; gam74_=ss_out(388) ; k75=ss_out(389) ; gam75_=ss_out(390) ; k76=ss_out(391) ; gam76_=ss_out(392) ; k77=ss_out(393) ; gam77_=ss_out(394) ; k78=ss_out(395) ; gam78_=ss_out(396) ; k79=ss_out(397) ; gam79_=ss_out(398) ; n=ss_out(479) ; n_=ss_out(480) ; c=ss_out(481) ; k=ss_out(482) ; w=ss_out(483) ; r=ss_out(484) ; R=ss_out(485) ; y=ss_out(486) ; a=ss_out(487) ; g=ss_out(488) ; tax=ss_out(489) ; tran=ss_out(490) ; dy=ss_out(491) ; check1=ss_out(492) ; resid_k=ss_out(493) ; d=ss_out(494) ; pr=ss_out(495) ; b=ss_out(496) ; l=ss_out(497) ; 

@# for j in 8 : 17
    k@{j} = 0.4 ;
@# endfor

rr = rr_vec_(1) ;
tau = 0 ;

Tstar = life_exp_at_65(1) ;

@# for j in 1 : N_g_ben-1
    ret_aux@{j} = 0 ;
@# endfor

@# for j in N_g_ben : N_g-1
    ret_aux@{j} = 1 ;
@# endfor

@# for j in 1:N_g
    v@{j}_ = vvec_end(@{j},1) ;
@# endfor

@# for j in 1 : N_g-1
    gam@{j}_ = gam_mat(@{j},1) ;
@# endfor

@# for j in 1 : N_g
    a@{j}_ = amat(@{j},1) ;
@# endfor

tau = 0.05 ;

end ;

steady(maxit=10000) ; 

//******************//
// Set ending value //
//******************//

load ss_out_bc_2070
ss_out=ss_out;

endval ;

n1=ss_out(1) ; n1_=ss_out(2) ; n2=ss_out(3) ; n2_=ss_out(4) ; n3=ss_out(5) ; n3_=ss_out(6) ; n4=ss_out(7) ; n4_=ss_out(8) ; n5=ss_out(9) ; n5_=ss_out(10) ; n6=ss_out(11) ; n6_=ss_out(12) ; n7=ss_out(13) ; n7_=ss_out(14) ; n8=ss_out(15) ; n8_=ss_out(16) ; n9=ss_out(17) ; n9_=ss_out(18) ; n10=ss_out(19) ; n10_=ss_out(20) ; n11=ss_out(21) ; n11_=ss_out(22) ; n12=ss_out(23) ; n12_=ss_out(24) ; n13=ss_out(25) ; n13_=ss_out(26) ; n14=ss_out(27) ; n14_=ss_out(28) ; n15=ss_out(29) ; n15_=ss_out(30) ; n16=ss_out(31) ; n16_=ss_out(32) ; n17=ss_out(33) ; n17_=ss_out(34) ; n18=ss_out(35) ; n18_=ss_out(36) ; n19=ss_out(37) ; n19_=ss_out(38) ; n20=ss_out(39) ; n20_=ss_out(40) ; n21=ss_out(41) ; n21_=ss_out(42) ; n22=ss_out(43) ; n22_=ss_out(44) ; n23=ss_out(45) ; n23_=ss_out(46) ; n24=ss_out(47) ; n24_=ss_out(48) ; n25=ss_out(49) ; n25_=ss_out(50) ; n26=ss_out(51) ; n26_=ss_out(52) ; n27=ss_out(53) ; n27_=ss_out(54) ; n28=ss_out(55) ; n28_=ss_out(56) ; n29=ss_out(57) ; n29_=ss_out(58) ; n30=ss_out(59) ; n30_=ss_out(60) ; n31=ss_out(61) ; n31_=ss_out(62) ; n32=ss_out(63) ; n32_=ss_out(64) ; n33=ss_out(65) ; n33_=ss_out(66) ; n34=ss_out(67) ; n34_=ss_out(68) ; n35=ss_out(69) ; n35_=ss_out(70) ; n36=ss_out(71) ; n36_=ss_out(72) ; n37=ss_out(73) ; n37_=ss_out(74) ; n38=ss_out(75) ; n38_=ss_out(76) ; n39=ss_out(77) ; n39_=ss_out(78) ; n40=ss_out(79) ; n40_=ss_out(80) ; n41=ss_out(81) ; n41_=ss_out(82) ; n42=ss_out(83) ; n42_=ss_out(84) ; n43=ss_out(85) ; n43_=ss_out(86) ; n44=ss_out(87) ; n44_=ss_out(88) ; n45=ss_out(89) ; n45_=ss_out(90) ; n46=ss_out(91) ; n46_=ss_out(92) ; n47=ss_out(93) ; n47_=ss_out(94) ; n48=ss_out(95) ; n48_=ss_out(96) ; n49=ss_out(97) ; n49_=ss_out(98) ; n50=ss_out(99) ; n50_=ss_out(100) ; n51=ss_out(101) ; n51_=ss_out(102) ; n52=ss_out(103) ; n52_=ss_out(104) ; n53=ss_out(105) ; n53_=ss_out(106) ; n54=ss_out(107) ; n54_=ss_out(108) ; n55=ss_out(109) ; n55_=ss_out(110) ; n56=ss_out(111) ; n56_=ss_out(112) ; n57=ss_out(113) ; n57_=ss_out(114) ; n58=ss_out(115) ; n58_=ss_out(116) ; n59=ss_out(117) ; n59_=ss_out(118) ; n60=ss_out(119) ; n60_=ss_out(120) ; n61=ss_out(121) ; n61_=ss_out(122) ; n62=ss_out(123) ; n62_=ss_out(124) ; n63=ss_out(125) ; n63_=ss_out(126) ; n64=ss_out(127) ; n64_=ss_out(128) ; n65=ss_out(129) ; n65_=ss_out(130) ; n66=ss_out(131) ; n66_=ss_out(132) ; n67=ss_out(133) ; n67_=ss_out(134) ; n68=ss_out(135) ; n68_=ss_out(136) ; n69=ss_out(137) ; n69_=ss_out(138) ; n70=ss_out(139) ; n70_=ss_out(140) ; n71=ss_out(141) ; n71_=ss_out(142) ; n72=ss_out(143) ; n72_=ss_out(144) ; n73=ss_out(145) ; n73_=ss_out(146) ; n74=ss_out(147) ; n74_=ss_out(148) ; n75=ss_out(149) ; n75_=ss_out(150) ; n76=ss_out(151) ; n76_=ss_out(152) ; n77=ss_out(153) ; n77_=ss_out(154) ; n78=ss_out(155) ; n78_=ss_out(156) ; n79=ss_out(157) ; n79_=ss_out(158) ; n80=ss_out(159) ; n80_=ss_out(160) ; c1=ss_out(161) ; c2=ss_out(162) ; c3=ss_out(163) ; c4=ss_out(164) ; c5=ss_out(165) ; c6=ss_out(166) ; c7=ss_out(167) ; c8=ss_out(168) ; c9=ss_out(169) ; c10=ss_out(170) ; c11=ss_out(171) ; c12=ss_out(172) ; c13=ss_out(173) ; c14=ss_out(174) ; c15=ss_out(175) ; c16=ss_out(176) ; c17=ss_out(177) ; c18=ss_out(178) ; c19=ss_out(179) ; c20=ss_out(180) ; c21=ss_out(181) ; c22=ss_out(182) ; c23=ss_out(183) ; c24=ss_out(184) ; c25=ss_out(185) ; c26=ss_out(186) ; c27=ss_out(187) ; c28=ss_out(188) ; c29=ss_out(189) ; c30=ss_out(190) ; c31=ss_out(191) ; c32=ss_out(192) ; c33=ss_out(193) ; c34=ss_out(194) ; c35=ss_out(195) ; c36=ss_out(196) ; c37=ss_out(197) ; c38=ss_out(198) ; c39=ss_out(199) ; c40=ss_out(200) ; c41=ss_out(201) ; c42=ss_out(202) ; c43=ss_out(203) ; c44=ss_out(204) ; c45=ss_out(205) ; c46=ss_out(206) ; c47=ss_out(207) ; c48=ss_out(208) ; c49=ss_out(209) ; c50=ss_out(210) ; c51=ss_out(211) ; c52=ss_out(212) ; c53=ss_out(213) ; c54=ss_out(214) ; c55=ss_out(215) ; c56=ss_out(216) ; c57=ss_out(217) ; c58=ss_out(218) ; c59=ss_out(219) ; c60=ss_out(220) ; c61=ss_out(221) ; c62=ss_out(222) ; c63=ss_out(223) ; c64=ss_out(224) ; c65=ss_out(225) ; c66=ss_out(226) ; c67=ss_out(227) ; c68=ss_out(228) ; c69=ss_out(229) ; c70=ss_out(230) ; c71=ss_out(231) ; c72=ss_out(232) ; c73=ss_out(233) ; c74=ss_out(234) ; c75=ss_out(235) ; c76=ss_out(236) ; c77=ss_out(237) ; c78=ss_out(238) ; c79=ss_out(239) ; c80=ss_out(240) ; k1=ss_out(241) ; gam1_=ss_out(242) ; k2=ss_out(243) ; gam2_=ss_out(244) ; k3=ss_out(245) ; gam3_=ss_out(246) ; k4=ss_out(247) ; gam4_=ss_out(248) ; k5=ss_out(249) ; gam5_=ss_out(250) ; k6=ss_out(251) ; gam6_=ss_out(252) ; k7=ss_out(253) ; gam7_=ss_out(254) ; k8=ss_out(255) ; gam8_=ss_out(256) ; k9=ss_out(257) ; gam9_=ss_out(258) ; k10=ss_out(259) ; gam10_=ss_out(260) ; k11=ss_out(261) ; gam11_=ss_out(262) ; k12=ss_out(263) ; gam12_=ss_out(264) ; k13=ss_out(265) ; gam13_=ss_out(266) ; k14=ss_out(267) ; gam14_=ss_out(268) ; k15=ss_out(269) ; gam15_=ss_out(270) ; k16=ss_out(271) ; gam16_=ss_out(272) ; k17=ss_out(273) ; gam17_=ss_out(274) ; k18=ss_out(275) ; gam18_=ss_out(276) ; k19=ss_out(277) ; gam19_=ss_out(278) ; k20=ss_out(279) ; gam20_=ss_out(280) ; k21=ss_out(281) ; gam21_=ss_out(282) ; k22=ss_out(283) ; gam22_=ss_out(284) ; k23=ss_out(285) ; gam23_=ss_out(286) ; k24=ss_out(287) ; gam24_=ss_out(288) ; k25=ss_out(289) ; gam25_=ss_out(290) ; k26=ss_out(291) ; gam26_=ss_out(292) ; k27=ss_out(293) ; gam27_=ss_out(294) ; k28=ss_out(295) ; gam28_=ss_out(296) ; k29=ss_out(297) ; gam29_=ss_out(298) ; k30=ss_out(299) ; gam30_=ss_out(300) ; k31=ss_out(301) ; gam31_=ss_out(302) ; k32=ss_out(303) ; gam32_=ss_out(304) ; k33=ss_out(305) ; gam33_=ss_out(306) ; k34=ss_out(307) ; gam34_=ss_out(308) ; k35=ss_out(309) ; gam35_=ss_out(310) ; k36=ss_out(311) ; gam36_=ss_out(312) ; k37=ss_out(313) ; gam37_=ss_out(314) ; k38=ss_out(315) ; gam38_=ss_out(316) ; k39=ss_out(317) ; gam39_=ss_out(318) ; k40=ss_out(319) ; gam40_=ss_out(320) ; k41=ss_out(321) ; gam41_=ss_out(322) ; k42=ss_out(323) ; gam42_=ss_out(324) ; k43=ss_out(325) ; gam43_=ss_out(326) ; k44=ss_out(327) ; gam44_=ss_out(328) ; k45=ss_out(329) ; gam45_=ss_out(330) ; k46=ss_out(331) ; gam46_=ss_out(332) ; k47=ss_out(333) ; gam47_=ss_out(334) ; k48=ss_out(335) ; gam48_=ss_out(336) ; k49=ss_out(337) ; gam49_=ss_out(338) ; k50=ss_out(339) ; gam50_=ss_out(340) ; k51=ss_out(341) ; gam51_=ss_out(342) ; k52=ss_out(343) ; gam52_=ss_out(344) ; k53=ss_out(345) ; gam53_=ss_out(346) ; k54=ss_out(347) ; gam54_=ss_out(348) ; k55=ss_out(349) ; gam55_=ss_out(350) ; k56=ss_out(351) ; gam56_=ss_out(352) ; k57=ss_out(353) ; gam57_=ss_out(354) ; k58=ss_out(355) ; gam58_=ss_out(356) ; k59=ss_out(357) ; gam59_=ss_out(358) ; k60=ss_out(359) ; gam60_=ss_out(360) ; k61=ss_out(361) ; gam61_=ss_out(362) ; k62=ss_out(363) ; gam62_=ss_out(364) ; k63=ss_out(365) ; gam63_=ss_out(366) ; k64=ss_out(367) ; gam64_=ss_out(368) ; k65=ss_out(369) ; gam65_=ss_out(370) ; k66=ss_out(371) ; gam66_=ss_out(372) ; k67=ss_out(373) ; gam67_=ss_out(374) ; k68=ss_out(375) ; gam68_=ss_out(376) ; k69=ss_out(377) ; gam69_=ss_out(378) ; k70=ss_out(379) ; gam70_=ss_out(380) ; k71=ss_out(381) ; gam71_=ss_out(382) ; k72=ss_out(383) ; gam72_=ss_out(384) ; k73=ss_out(385) ; gam73_=ss_out(386) ; k74=ss_out(387) ; gam74_=ss_out(388) ; k75=ss_out(389) ; gam75_=ss_out(390) ; k76=ss_out(391) ; gam76_=ss_out(392) ; k77=ss_out(393) ; gam77_=ss_out(394) ; k78=ss_out(395) ; gam78_=ss_out(396) ; k79=ss_out(397) ; gam79_=ss_out(398) ; v1_=ss_out(399) ; v2_=ss_out(400) ; v3_=ss_out(401) ; v4_=ss_out(402) ; v5_=ss_out(403) ; v6_=ss_out(404) ; v7_=ss_out(405) ; v8_=ss_out(406) ; v9_=ss_out(407) ; v10_=ss_out(408) ; v11_=ss_out(409) ; v12_=ss_out(410) ; v13_=ss_out(411) ; v14_=ss_out(412) ; v15_=ss_out(413) ; v16_=ss_out(414) ; v17_=ss_out(415) ; v18_=ss_out(416) ; v19_=ss_out(417) ; v20_=ss_out(418) ; v21_=ss_out(419) ; v22_=ss_out(420) ; v23_=ss_out(421) ; v24_=ss_out(422) ; v25_=ss_out(423) ; v26_=ss_out(424) ; v27_=ss_out(425) ; v28_=ss_out(426) ; v29_=ss_out(427) ; v30_=ss_out(428) ; v31_=ss_out(429) ; v32_=ss_out(430) ; v33_=ss_out(431) ; v34_=ss_out(432) ; v35_=ss_out(433) ; v36_=ss_out(434) ; v37_=ss_out(435) ; v38_=ss_out(436) ; v39_=ss_out(437) ; v40_=ss_out(438) ; v41_=ss_out(439) ; v42_=ss_out(440) ; v43_=ss_out(441) ; v44_=ss_out(442) ; v45_=ss_out(443) ; v46_=ss_out(444) ; v47_=ss_out(445) ; v48_=ss_out(446) ; v49_=ss_out(447) ; v50_=ss_out(448) ; v51_=ss_out(449) ; v52_=ss_out(450) ; v53_=ss_out(451) ; v54_=ss_out(452) ; v55_=ss_out(453) ; v56_=ss_out(454) ; v57_=ss_out(455) ; v58_=ss_out(456) ; v59_=ss_out(457) ; v60_=ss_out(458) ; v61_=ss_out(459) ; v62_=ss_out(460) ; v63_=ss_out(461) ; v64_=ss_out(462) ; v65_=ss_out(463) ; v66_=ss_out(464) ; v67_=ss_out(465) ; v68_=ss_out(466) ; v69_=ss_out(467) ; v70_=ss_out(468) ; v71_=ss_out(469) ; v72_=ss_out(470) ; v73_=ss_out(471) ; v74_=ss_out(472) ; v75_=ss_out(473) ; v76_=ss_out(474) ; v77_=ss_out(475) ; v78_=ss_out(476) ; v79_=ss_out(477) ; v80_=ss_out(478) ; n=ss_out(479) ; n_=ss_out(480) ; c=ss_out(481) ; k=ss_out(482) ; w=ss_out(483) ; r=ss_out(484) ; R=ss_out(485) ; y=ss_out(486) ; a=ss_out(487) ; g=ss_out(488) ; tax=ss_out(489) ; tran=ss_out(490) ; dy=ss_out(491) ; check1=ss_out(492) ; resid_k=ss_out(493) ; d=ss_out(494) ; pr=ss_out(495) ; b=ss_out(496) ; l=ss_out(497) ; //mu1=ss_out(498) ; mu2=ss_out(499) ; mu3=ss_out(500) ; mu4=ss_out(501) ; mu5=ss_out(502) ; mu6=ss_out(503) ; mu7=ss_out(504) ; mu8=ss_out(505) ; mu9=ss_out(506) ; mu10=ss_out(507) ; mu11=ss_out(508) ; mu12=ss_out(509) ; mu13=ss_out(510) ; mu14=ss_out(511) ; mu15=ss_out(512) ; mu16=ss_out(513) ; mu17=ss_out(514) ; mu18=ss_out(515) ; mu19=ss_out(516) ; mu20=ss_out(517) ; mu21=ss_out(518) ; mu22=ss_out(519) ; mu23=ss_out(520) ; mu24=ss_out(521) ; mu25=ss_out(522) ; mu26=ss_out(523) ; mu27=ss_out(524) ; mu28=ss_out(525) ; mu29=ss_out(526) ; mu30=ss_out(527) ; mu31=ss_out(528) ; mu32=ss_out(529) ; mu33=ss_out(530) ; mu34=ss_out(531) ; mu35=ss_out(532) ; mu36=ss_out(533) ; mu37=ss_out(534) ; mu38=ss_out(535) ; mu39=ss_out(536) ; mu40=ss_out(537) ; mu41=ss_out(538) ; mu42=ss_out(539) ; mu43=ss_out(540) ; mu44=ss_out(541) ; mu45=ss_out(542) ; mu46=ss_out(543) ; mu47=ss_out(544) ; mu48=ss_out(545) ; mu49=ss_out(546) ; mu50=ss_out(547) ; mu51=ss_out(548) ; mu52=ss_out(549) ; mu53=ss_out(550) ; mu54=ss_out(551) ; mu55=ss_out(552) ; mu56=ss_out(553) ; mu57=ss_out(554) ; mu58=ss_out(555) ; mu59=ss_out(556) ; mu60=ss_out(557) ; mu61=ss_out(558) ; mu62=ss_out(559) ; mu63=ss_out(560) ; mu64=ss_out(561) ; mu65=ss_out(562) ; mu66=ss_out(563) ; mu67=ss_out(564) ; mu68=ss_out(565) ; mu69=ss_out(566) ; mu70=ss_out(567) ; mu71=ss_out(568) ; mu72=ss_out(569) ; mu73=ss_out(570) ; mu74=ss_out(571) ; mu75=ss_out(572) ; mu76=ss_out(573) ; mu77=ss_out(574) ; mu78=ss_out(575) ; mu79=ss_out(576) ; mu80=ss_out(577) ; 
b = 0 ;

@# for j in 8 : 12
    k@{j} = 0.5 ;
@# endfor

@# for j in 13 : 16
    k@{j} = 3 ;
@# endfor

@# for j in 17 : 22
    k@{j} = 4 ;
@# endfor

rr = rr_vec_(1) ;

Tstar = life_exp_at_65(1) ;

@# for j in 1 : N_g_ben-1
    ret_aux@{j} = 0 ;
@# endfor

@# for j in N_g_ben : N_g-1
    ret_aux@{j} = 1 ;
@# endfor

@# for j in 1:N_g
    v@{j}_ = vvec_end(@{j},131) ;
@# endfor

@# for j in 1 : N_g-1
    gam@{j}_ = gam_mat(@{j},131) ;
@# endfor

@# for j in 1 : N_g
    a@{j}_ = amat(@{j},131) ;
@# endfor

tau = 0.05 ;

end ;

steady(maxit=10000) ; 

//***********//
// Set shock //
//***********//

get_gam_probs

if longev_sh==0 ;
gam_mat(:,1:131) = repmat(gam_mat(:,1),[1 131]) ;
end

@# for j in 1 : N_g-1
    xx@{j} = 1*gam_mat(@{j},:)' ;
@# endfor

@# for j in 1 : N_g-1
    vvec_@{j} = 1*vvec_end(@{j},:)' ;
@# endfor

load nsh % computed from get_n_shocks
for ii=61:121
nshocks(ii) = nshocks(ii-1)*0.8 ;
end
nshocks = tv_nshocks*nshocks ;

Tstar_vec = (life_exp_at_65)' ;
rr_vec = rr_vec ;

get_ret_aux_vec 

@# for j in 1 : N_g-1
    ret_aux_vec@{j} = ret_aux_mat(@{j},:)' ;
@# endfor

@# for j in 1 : N_g
    a_vec@{j} = amat(@{j},:)' ;
@# endfor

shocks ;

@# for j in 1 : N_g
var a@{j}_ ;
    periods 1:131 ;
    values (a_vec@{j}) ;
@# endfor

var eps_n1 ;
    periods 11:131 ;
    values (nshocks) ;

@# for j in 1 : N_g-1
var gam@{j}_ ;
    periods 1:131 ;
    values (xx@{j}) ;
@# endfor

@# for j in 1 : N_g-1
var v@{j}_ ;
    periods 1:131 ;
    values (vvec_@{j}) ;
@# endfor

end ;

model_diagnostics ;

simul(periods=201,maxit=50) ;
